angular.module('inspinia',['uiSwitch']).controller('agentActivityQueryCtrl',function($scope,$http,i18nService,SweetAlert,$document){
    i18nService.setCurrentLang('zh-cn');  //设置语言为中文
    $scope.paginationOptions=angular.copy($scope.paginationOptions);

    $scope.openStatusSelect = [{text:"全部",value:null},{text:"打开",value:1},{text:"关闭",value:0}];
    $scope.openStatusStr=angular.toJson($scope.openStatusSelect);
    //清空
    $scope.clear=function(){
        $scope.info={openStatus:null};
    };
    $scope.clear();

    $scope.userGrid={                           //配置表格
        data: 'result',
        paginationPageSize:10,                  //分页数量
        paginationPageSizes: [10,20,50,100],	//切换每页记录数
        useExternalPagination: true,		    //开启拓展名
        enableHorizontalScrollbar: true,        //横向滚动条
        enableVerticalScrollbar : true,  		//纵向滚动条
        columnDefs:[                           //表格数据
            { field: 'actCode',displayName:'活动编号',width:180},
            { field: 'actName',displayName:'活动名称',width:180},
            { field: 'actBeginTime',displayName:'活动时间-开始时间',cellFilter: 'date:"yyyy-MM-dd HH:mm:ss"',width:220},
            { field: 'actEndTime',displayName:'活动时间-结束时间',cellFilter: 'date:"yyyy-MM-dd HH:mm:ss"',width:220},
            {field: 'openStatus',displayName: '活动状态',width: 180,cellTemplate:
                    '<span ng-show="grid.appScope.hasPermit(\'agentActivityAction.saveSwitch\')"><switch class="switch switch-s" ng-model="row.entity.openStatus" ng-true-value="1" ng-false-value="0" ng-change="grid.appScope.updateStatus(row.entity)" /></span>'
                    +'<span ng-show="!grid.appScope.hasPermit(\'agentActivityAction.saveSwitch\')"> <span ng-show="row.entity.openStatus==1">开启</span><span ng-show="row.entity.openStatus==0">关闭</span></span>'
            },
            { field: 'createTime',displayName:'创建时间',cellFilter: 'date:"yyyy-MM-dd HH:mm:ss"',width:180},
            { field: 'operator',displayName:'创建人',width:180},
            { field: 'id',displayName:'操作',width:280,pinnedRight:true, cellTemplate:
                    '<div class="lh30">'+
                        '<a target="_blank" ui-sref="agent.agentActivitySave({actCode:row.entity.actCode,isDetail:1})">详情 </a> ' +
                        '<a ng-show="grid.appScope.hasPermit(\'agentActivityAction.editAgentActivity\')" target="_blank" ui-sref="agent.agentActivitySave({actCode:row.entity.actCode,isDetail:2})"> | 修改</a> ' +
                        '<a ng-show="grid.appScope.hasPermit(\'agentActivityAction.deleteAgentActivity\')&&row.entity.openStatus==0" ng-click="grid.appScope.deleteInfo(row.entity)"> | 删除</a> ' +
                        '<a ng-show="grid.appScope.hasPermit(\'agentActivityAction.selectAgentActivityData\')" target="_blank" ui-sref="agent.agentActivityData({actCode:row.entity.actCode})"> | 活动数据</a> ' +
                        '<a ng-show="grid.appScope.hasPermit(\'agentActivityAction.selectAgentRankingList\')" target="_blank" ui-sref="agent.agentRankingList({actCode:row.entity.actCode})"> | 榜单排名</a> ' +
                    '</div>'
            }
        ],
        onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
            gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                $scope.paginationOptions.pageNo = newPage;
                $scope.paginationOptions.pageSize = pageSize;
                $scope.query();
            });
        }
    };
    $scope.query=function(){
        if ($scope.loadImg) {
            return;
        }
        $scope.loadImg = true;
        var data = {
            info:angular.toJson($scope.info),
            pageNo:$scope.paginationOptions.pageNo,
            pageSize:$scope.paginationOptions.pageSize
        };
        $http.post("agentActivityAction/selectAll",data,$scope.postCfgAllCurrency)
            .success(function (data) {
                if (data.status) {
                    $scope.result=data.page.result;
                    $scope.userGrid.totalItems = data.page.totalCount;
                } else {
                    $scope.notice(data.msg);
                }
                $scope.loadImg = false;
            }).error(function(data){
                 $scope.notice(data.msg);
                 $scope.loadImg = false;
            });
    };
    $scope.query();
    //开启
    $scope.updateStatus = function(entity){
        var str="";
        var sta=0;
        if(entity.openStatus){
            str="确认打开?";
            sta=1;
        }else{
            str="确认关闭?";
        }
        SweetAlert.swal({
                title: str,
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "提交",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    $http.post("agentActivityAction/saveSwitch","id="+entity.id+"&sta="+sta,
                        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
                        .success(function(data){
                            if(data.status){
                                $scope.notice(data.msg);
                                $scope.query();
                            }else{
                                entity.openStatus = !entity.openStatus;
                                $scope.notice(data.msg);
                            }
                        })
                        .error(function(data){
                            entity.openStatus = !entity.openStatus;
                        });

                }else{
                    entity.openStatus = !entity.openStatus;
                }
            });
    };

    //删除
    $scope.deleteInfo = function(entity){
        SweetAlert.swal({
                title: "确定删除？",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "提交",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    $http.post("agentActivityAction/deleteAgentActivity","actCode="+entity.actCode,
                        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
                        .success(function(data){
                            if(data.status){
                                $scope.notice(data.msg);
                                $scope.query();
                            }else{
                                $scope.notice(data.msg);
                            }
                        });
                }
            });
    };
    //页面绑定回车事件
    $document.bind("keypress", function(event) {
        $scope.$apply(function (){
            if(event.keyCode == 13){
                $scope.query();
            }
        })
    });
});